package com.que.owen.service.impl;

import com.que.owen.entity.Taxonomy;
import com.que.owen.model.PageQuery;
import com.que.owen.service.TestMongoService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName TestMongoServiceImpl
 * @Author Owen.Que
 * @Description
 * @Date 8/29/2018 15:48
 **/
@Service
@Transactional
public class TestMongoServiceImpl implements TestMongoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void testOperation() {
        test2();
    }

    @Override
    public String insertTestData(String collection, Long count) {
        int cnt=0;
        List list=new ArrayList();
        for(int i=0;i<100;i++){
            for(int j=101;j<count;j++){
                cnt++;
                Map map=new HashMap();
                map.put("_id", new ObjectId());
                map.put("id",String.format("id-i(%s)-j(%s)",i,j));
                map.put("name",String.format("name-i(%s)-j(%s)",i,j));
                map.put("i",i);
                map.put("j",j);
                map.put("uuid",UUID.randomUUID().toString());
                list.add(map);
                if(cnt>=1000){
                    cnt=0;
                    mongoTemplate.insert(list,collection);
                    list=new ArrayList();
                }
            }
        }
        if(list.size()>0){
            mongoTemplate.insert(list,collection);
        }

        return "success";
    }


    @Override
    public String paginationGetByQuery(Query query, PageQuery pq) {
        long l1 = System.currentTimeMillis();
        List<Map> dataList = mongoTemplate.find(query.with(pq),Map.class, "autodata");
        long l2 = System.currentTimeMillis();
        String msg="use query cost "+(l2-l1)+"ms, get data count "+dataList.size()+"  name="+dataList.get(0).get("name");
        return msg;
    }

    @Override
    public String paginationGetByAggregate(Criteria criteria, PageQuery pq) {

        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.skip(pq.getOffset()*1L),
                Aggregation.limit(pq.getPageSize()));
        long l1 = System.currentTimeMillis();
        AggregationResults<Map> temp = mongoTemplate.aggregate(aggregation, "autodata", Map.class);
        long l2 = System.currentTimeMillis();
        String msg="use query cost "+(l2-l1)+"ms, get data count "+temp.getMappedResults().size();
        System.out.println(msg);
        return msg;
    }

    @Override
    public String testMock(Integer pageNo) {
        return "from real method";
    }

    private void test2() {
        Query query= Query.query(Criteria.where("pid").is("11015-4"));
        PageQuery pq=new PageQuery();
        pq.setPageSize(2);
        pq.setCurrentPage(4);
        List<Sort.Order> orders = new ArrayList<Sort.Order>();  //排序
        orders.add(new Sort.Order(Sort.Direction.DESC, "name"));
        Sort sort= new Sort(orders);
        pq.setSort(sort);
        List<Taxonomy> taxonomyList = mongoTemplate.find(query.with(pq),Taxonomy.class, "251");
        Optional.ofNullable(taxonomyList).ifPresent(l->{l.forEach(t->{
            System.out.println(t);
        });});
    }

    private void test1() {
        Query query= Query.query(Criteria.where("id").is("11011-2"));
        List<Taxonomy> taxonomyList = mongoTemplate.find(query, Taxonomy.class, "251");
        Optional.ofNullable(taxonomyList).ifPresent(l->{l.forEach(t->{
            System.out.println(t);
        });});
    }
}
